#include <pd_gpio.h>
#include <hd_oled_protocol_i2c.h>
#include <hd_led.h>
#include <pd_timer_ch.h>
#include <pd_timer.h>
#include <pd_timer_oc.h>
#include <pd_timer_pwm.h>
#include <hd_oled.h>
#include <delay.h>
#include <pd_timer_ic.h>
#include <pd_i2c_hd.h>
#include "main.h"

#define ARRAY_SIZE(x)       sizeof(x) / sizeof(x[0])
#define USE_HARDWARE_SPI    1
#define USE_HARDWARE_I2C    1
OLED oled;

extern void SystemClock_Config(void);

void oled_instance(OLED *oled);

void timer_pwm_init(uint32_t freq, uint8_t duty);

int main(void) {
    SystemClock_Config();

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

    oled_instance(&oled);

    OLED_printf(&oled, 0, 0, "TIM IC");
    OLED_printf(&oled, 0, 16, "Freq:00000000Hz");
    OLED_printf(&oled, 0, 32, "duty:");
    OLED_refresh(&oled);


    // Set Pin PC6-AF2-TIM3_CH1 as IC channel, TIM3_CH2 as indirect channel
    GPIO gpio;
    GPIO_constructor(&gpio, GPIOC, LL_GPIO_PIN_6);
    gpio.Mode = LL_GPIO_MODE_ALTERNATE;
    gpio.Alternate = LL_GPIO_AF_2;
    uint32_t channel[] = {LL_TIM_CHANNEL_CH1, LL_TIM_CHANNEL_CH2};
    TimerCh timerChannel;
    TimerCh_constructor(&timerChannel, &gpio, 1, channel, 2);

    Timer timer;
    Timer_constructor(&timer, TIM3, 84 - 1, 65536 - 1);
    TimerIC timerIC;
    TimerIC_constructor(&timerIC, &timer, &timerChannel);
    TimerIC_init(&timerIC, 1);

    timer_pwm_init(12000, 30);

    while (1) {
        OLED_printf(&oled, 40, 16, "%8d", TimerIC_getFreq(&timerIC));
        OLED_printf(&oled, 40, 32, "%2d", TimerIC_getDuty(&timerIC));
        OLED_refresh(&oled);
        delay_ms(100);
    }
}


void timer_pwm_init(uint32_t freq, uint8_t duty) {
    // GPIO and Channel: PB6 -- TIM4_CH1; PB7 -- TIM4_CH2; PB8 -- TIM4_CH3; PB9 -- TIM4_CH4
    GPIO gpio;
    GPIO_constructor(&gpio,GPIOB, LL_GPIO_PIN_6 | LL_GPIO_PIN_7 | LL_GPIO_PIN_8 | LL_GPIO_PIN_9);
    gpio.Mode = LL_GPIO_MODE_ALTERNATE;
    gpio.Alternate = LL_GPIO_AF_2;
    uint32_t channel[] = {LL_TIM_CHANNEL_CH1, LL_TIM_CHANNEL_CH2, LL_TIM_CHANNEL_CH3, LL_TIM_CHANNEL_CH4};
    TimerCh timerChannel;
    TimerCh_constructor(&timerChannel, &gpio, 1, channel, 4);

    // resolution = 1%, freq = CK_PSC / (PSC + 1) / (ARR + 1)
    Timer timer;
    Timer_constructor(&timer, TIM4, 84000000 / 100 / freq - 1, 100 - 1);
    // CCR = Duty * (ARR + 1)
    TimerOC timerOC;
    TimerOC_constructor(&timerOC, &timer, &timerChannel, duty * 100);
    TimerPWM timerPWM;
    TimerPWM_constructor(&timerPWM, &timer, &timerOC);
    TimerPWM_init(&timerPWM);

    TimerPWM_setDuty(&timerPWM, LL_TIM_CHANNEL_CH1, duty + 10);
    TimerPWM_setDuty(&timerPWM, LL_TIM_CHANNEL_CH2, duty + 20);
    TimerPWM_setDuty(&timerPWM, LL_TIM_CHANNEL_CH3, duty + 30);
    TimerPWM_setDuty(&timerPWM, LL_TIM_CHANNEL_CH4, duty + 40);
}

void oled_instance(OLED *oled) {
    // I2C gpio init
    static GPIO oledSckGPIO, oledSdaGPIO;
    GPIO_constructor(&oledSckGPIO, GPIOB, LL_GPIO_PIN_10); // I2Cx_SCK
    GPIO_constructor(&oledSdaGPIO, GPIOB, LL_GPIO_PIN_11); // I2Cx_SDA

#ifdef USE_HARDWARE_I2C
    static I2CHardware i2c2;
    I2CHardware_constructor(&i2c2, I2C2, &oledSckGPIO, &oledSdaGPIO);
#elif USE_SIMULATE_I2C
    static I2CSimulate i2c2;
    I2CSimulate_constructor(&i2c2, &oledSckGPIO, &oledSdaGPIO);
#endif
    I2C_init((I2C *)&i2c2);

    // OLED protocol of I2C
    static OLEDProtocolI2C oledProtocolI2c;
    OLEDProtocolI2C_constructor(&oledProtocolI2c, (I2C *)&i2c2);

    // OLED font instance.
    static OLEDFont oledFont;
    OLEDFont_contructor(&oledFont, NULL);

    // OLED instance of 4-PIN(I2C)
    OLED_constructor(oled, (OLEDProtocol *)&oledProtocolI2c, &oledFont);

    OLED_init(oled);

    OLED_clear(oled);
    OLED_refresh(oled);
}
